﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using FWPDocumentation.Common.DbTools;
using FWPDocumentation.Entities;
using FWPDocumentation.VisualForms;
using MySql.Data.MySqlClient;

namespace FWPDocumentation
{
    public partial class FormAddStudent : Form
    {
        private Institute selectedInst;
        private Faculty selectedFaculty;
        private Group selectedGroup;
        private String lastSuccessfullnumber;
        private List<int> marks = new List<int>();
        private DataGridView dataGrid;
 

        public FormAddStudent()
        {
            
            InitializeComponent();
            comboBox1.Items.AddRange(Institute.GetInstitutes().ToArray());

        }

        public FormAddStudent(DataGridView dgvView)
        {
            dataGrid = dgvView;
            InitializeComponent();
            comboBox1.Items.AddRange(Institute.GetInstitutes().ToArray());

        }


        private void Button1Click(object sender, EventArgs e)
        {
            if(comboBox1.SelectedIndex>-1&&comboBox2.SelectedIndex>-1&&comboBox3.SelectedIndex>-1&&
                textBox1.Text!=""&&textBox2.Text!=""&&
                textBox3.Text!=""&&textBox4.Text!=""&&textBox5.Text!=""&&textBox6.Text!=""&&textBox7.Text!=""&& textBox8.Text!=""&&
                textBox8.Text.Length==10)
            {
                

                Student.CreateStudent(textBox1.Text, textBox2.Text, textBox3.Text, selectedGroup,
                                      long.Parse(textBox8.Text), Double.Parse(textBox6.Text),int.Parse(textBox4.Text), checkBox3.Checked,checkBox4.Checked,
                                      checkBox1.Checked, checkBox2.Checked);



                dataGrid.Columns.Clear();
                dataGrid.Refresh();
                var ds = new DataSet();
                var da = new MySqlDataAdapter(new MySqlCommand(
                    "SELECT Surname as Фамилия, Name as Имя, MiddleName as Отчество, Institute as Институт, Faculty as Факультет, `group` as Группа, GPA as `Средний бал`, " +
                    "TEST as `Тест`, MedicalCommission as `Мед. комиссия`, ProfSelection as `Проф. отбор`, PhysSelection as `ФизО`, Payment as `Оплата`, " +
                    "AdmissionYear as `Год поступления` FROM maininfo,institute, faculty, `group`, admission WHERE maininfo.Group_idGroup = `group`.idGroup " +
                    "AND `group`.Faculty_idFaculty = faculty.idFaculty AND faculty.Institute_idInstitute = institute.idInstitute " +
                    "AND maininfo.idMainInfo = admission.MainInfo_idMainInfo;", DbIface.Connection));
                da.Fill(ds, "maininfo");
                DataView view = ds.Tables["maininfo"].AsDataView();
                dataGrid.DataSource = view;
                dataGrid.Update();
                Close();
                
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedInst = (Institute) comboBox1.SelectedItem;
            comboBox2.Enabled = true;
            comboBox2.Items.AddRange(selectedInst.getFacultyes().ToArray());
            comboBox3.Enabled = false;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedFaculty = (Faculty) comboBox2.SelectedItem;
            comboBox3.Enabled = true;
            comboBox3.Items.AddRange(selectedFaculty.getGroups().ToArray());
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedGroup = (Group) comboBox3.SelectedItem;
        }

        private void TextBox4TextChanged(object sender, EventArgs e)
        {
            if (textBox4.Text != "")
            {
                try
                {
                    int numb = Convert.ToInt32(textBox4.Text.ToString());
                    if (numb <= 200 && numb > 0)
                    {
                        lastSuccessfullnumber = numb.ToString();
                    }
                    else
                    {
                        textBox4.Text = lastSuccessfullnumber;
                    }
                }
                catch (Exception ex)
                {
                    textBox4.Text = lastSuccessfullnumber;
                }
            }
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {

            if(e.KeyChar=='3'||e.KeyChar=='4'||e.KeyChar=='5')
            {
                textBox5.Text += e.KeyChar+" ";
                textBox5.SelectionStart = textBox5.TextLength;
                textBox5.SelectionLength = 0;
                
            }
            if (e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;
            }
            
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            char[] marks = textBox5.Text.ToCharArray();
            double mark = 0;
            int count=0;
            foreach(char c in marks)
            {
                if(Char.IsDigit(c))
                {
                    count++;
                    mark += Char.GetNumericValue(c);
                }
            }
            mark /= count;
            textBox6.Text = String.Format("{0:0.##}", mark);
            textBox7.Text = count.ToString();
            if (textBox5.Text.ToString() == "")
                textBox6.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }


    }
}
